<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/7
 * Time: 8:38
 */

namespace common\business\qywx;

use common\result\ActionResult;
use Yii;
use common\models\qywx\QywxAccessToken;

class QywxAccessTokenApiHandler extends QywxApiHandler
{
    /**
     * @param $corpid string
     * @param $corpsecret string
     * @param callback|null $writeBackHandler
     * @return bool
     */
    public function createOnQywx($corpid, $corpsecret, $writeBackHandler = null){
        $url = $this->baseUrl . '/gettoken';
        $params = [
            'corpid' => $corpid,
            'corpsecret' => $corpsecret,
        ];
        return $this->getJson($url, $params, $writeBackHandler);
    }

    /**
     * @param $obj QywxAccessToken
     * @return ActionResult
     */
    public function createQywxAccessToken($obj){
        return $this->create($obj);
    }

    /**
     * @param QywxAccessToken $newObj
     * @param QywxAccessToken $oldObj
     * @return ActionResult
     */
    public function updateQywxAccessToken($newObj, $oldObj = null){
        if (!$oldObj){
            $oldObj = QywxAccessToken::findOne([
                'userid' => $newObj->userid
            ]);
        }
        return $this->update($newObj, $oldObj, [
            'name',
            'number',
            'gender',
            'birthday',
            'email',
            'mobile',
            'id_card_num',
            'native_place',
            'blood_type',
            'wed_status',
            'nationality',
            'folk',
            'hklx',
            'hr_id',
            'weixin',
            'position_name',
            'labor_relation_state',
        ]);
    }
}